/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell.value.support;

import org.dom4j.Element;

import com.reportgear.core.api.XmlSerializer;
import com.reportgear.report.core.api.compare.ComparatorUtils;
import com.sot.core.util.NumberUtils;

/**
 * 通用对象
 * 
 * @version 1.0 2011-4-5
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class CommonObject implements Comparable<CommonObject>, XmlSerializer {
	// Id
	private int id = 0;
	// 父Id
	private int parentId = 0;
	// 名称
	private String name = "";
	// 代码
	private String code = "";

	public CommonObject() {
	}

	public CommonObject(int id, int parentId, String code, String name) {
		this.id = id;
		this.parentId = parentId;
		this.code = code;
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + parentId;

		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CommonObject other = (CommonObject) obj;

		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (parentId != other.parentId)
			return false;

		return true;
	}

	/**
	 * 复制属性
	 * 
	 * @param co
	 *            通用对象
	 */
	public void copyProperties(CommonObject co) {
		if (co == null) {
			return;
		}

		this.id = co.id;
		this.parentId = co.parentId;
		this.code = co.code;
		this.name = co.name;
	}

	@Override
	public String toString() {
		return this.name;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public int getParentId() {
		return parentId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	@Override
	public int compareTo(CommonObject o) {
		return ComparatorUtils.compare(this.getCode(), o.getCode());
	}

	@Override
	public void read(Element parent) {
		this.setId(NumberUtils.convertToInteger(parent.elementTextTrim("id")));
		this.setParentId(NumberUtils.convertToInteger(parent.elementTextTrim("parentId")));
		this.setName(parent.elementTextTrim("name"));
		this.setCode(parent.elementTextTrim("code"));
	}

	@Override
	public void write(Element parent) {
		Element el = parent.addElement("id");
		el.addText(String.valueOf(this.getId()));

		el = parent.addElement("parentId");
		el.addText(String.valueOf(this.getParentId()));

		el = parent.addElement("name");
		el.addText(this.getName());

		el = parent.addElement("code");
		el.addText(this.getCode());
	}
}
